// import Nevow.Athena
// import Divmod.Runtime

Meercat.Reducer.List = Nevow.Athena.Widget.subclass('Meercat.Reducer.List');
Meercat.Reducer.Types = Nevow.Athena.Widget.subclass('Meercat.Reducer.Type');
Meercat.Reducer.Form = Meercat.SubForm.subclass('Meercat.Reducer.Form');

Meercat.Reducer.List.methods(
function loaded(self) {
    self.refresh();
},
function refresh(self) {
    d = self.callRemote('getReducerList');
    d.addCallback(function (result) {
        var ul = jQuery('#' + escapeId(self.node.id) + ' > ul.ReducerList');
        ul.empty();
        
        for (i = 0; i < result.length; i++) {
            var mapper = '<li>' + result[i]['type'] + '<ul>';
            
            for (j = 0; j < result[i]['formats'].length; j++) {
                mapper = mapper + '<li>' + result[i]['formats'][j] + '</li>';
            }
            
            ul.append(mapper + '</ul><a href="#" ' + 
                'onclick="Nevow.Athena.Widget.get(this).removeReducer(\'' + 
                result[i]['hash'] + '\');">[x]</a></li>');
        }
    });
    
    return d;
},
function removeReducer(self, _hash) {
    d = self.callRemote('deleteReducer', _hash);
    d.addCallback(function (result) {
        self.refresh();
    });
    
    d.addErrback(function (failure) {
        alert('Error removing harvester: ' + failure);
    });
},
function addReducerElement(self) {
	var d = self.callRemote('getAddReducerElement');
	
	d.addCallback(function (le) {
		var d2 = self.addChildWidgetFromWidgetInfo(le);
		d2.addCallback(function (widget) {
			jQuery('#main-content').append(widget.node);
		});
	});
	
	d.addErrback(function (failure) {
	    alert('Error creating an add storage form');
	});
});

Meercat.Reducer.Types.methods(
function selectType(self) {
    var d = self.callRemote('getReducerForm',
                            self.firstNodeByAttribute('name','type').value);
    
    for (i = 0; i < self.childWidgets.length; i++) {
	    self.childWidgets[i].remove();
	}
    
    d.addCallback(function (result) {
        if (result) {
            var d2 = self.addChildWidgetFromWidgetInfo(result);
            d2.addCallback(function (widget) {
                jQuery('#' + escapeId(self.node.id)).append(widget.node);
            });
            d2.addErrback(function (failre) {
                alert('Error adding child widget: ' + failure);
            });
        }
    });
    d.addErrback(function (failure) {
        alert('Error selecting the job type');
    });
},
function remove(self) {
    for (i = 0; i < self.childWidgets.length; i++) {
	    self.childWidgets[i].remove();
	}
    
    var d = self.detach();
    d.addCallback(function (result) {
        jQuery('#' + escapeId(self.node.id)).remove();
    });
    
    d.addErrback(function (failure) {
        alert('Error detaching the add harvester form: ' + failure);
    });
});

Meercat.Reducer.Form.methods(
function addFormatInput(self) {
    var d = self.callRemote('getFlatFormatInput');
    
    d.addCallback(function (result) {
        jQuery('#' + escapeId(self.node.id) + ' li.mapperFormats a').before(result);
    });
    
    d.addErrback(function (failure) {
        alert('Unable to add a format input');
    });
});